#include <iostream>
using namespace std;
class AutoRelease;

class Singleton{
public:
    friend AutoRelease;
    static Singleton * getInstance(){
        if(_pInstance == nullptr){
            _pInstance == new Singleton();
        }
        return _pInstance;
    }

private:
    static Singleton * _pInstance;
    Singleton(){
        cout << "Singleton()" << endl;
    }
    ~Singleton(){
        cout << "~Singleton()" << endl;
    }
};
Singleton * Singleton::_pInstance = nullptr;

class AutoRelease{
public:
    AutoRelease(Singleton * s)
    :_s(s)
    {
        cout << "AutoRelease(Signleton)" << endl;

    }

    ~AutoRelease(){
        cout << "~AutoRelease()" << endl;
        if(_s){
            delete _s;
            _s = nullptr;
        }
    }
private:
    Singleton * _s;
};

void test(){
    AutoRelease ar(Singleton::getInstance());
    AutoRelease ar2(Singleton::getInstance());

}

int main(int atgc, char * argv[])
{
    test();
    return 0;
}

